#!/usr/bin/env node

import { readFile, writeFile } from "fs-extra";
import { file } from "newrow-utils/dist/src/file";
import { tool } from "newrow-utils/dist/src/tool";
import { Structures } from "./Structures";
import * as path from "path";

const processPath = process.cwd();
const outputPath = path.join(processPath, 'output');

(async () => {
    let shaderFileList: string[] = [];
    await file.recurDir(processPath, filePath => {
        if (path.extname(filePath) != '.shader') return;
        if (path.dirname(filePath) == outputPath) return;
        shaderFileList.push(filePath);
    });

    let task = new tool.parallelTasks(100);
    task.Run(shaderFileList, async filePath => {
        let fileText = await readFile(filePath, 'UTF8');
        let shaderFile = new Structures.ShaderFile(fileText);
        try {
            await writeFile(filePath, shaderFile.shader.complete);
        } catch (error) {
            console.error(filePath);
            console.error(error);
        }
    });
})();